package com.czy.owner.ui.hotshop;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.api.AddCartApi;
import com.czy.owner.api.CollectionApi;
import com.czy.owner.api.GetCustomerApi;
import com.czy.owner.api.GoodsDetailApi;
import com.czy.owner.api.GoodsRemainCountApi;
import com.czy.owner.api.GoodsShareApi;
import com.czy.owner.api.ShoppingCartCountApi;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.DeliveryWayEntity;
import com.czy.owner.entity.GoodsDetailModel;
import com.czy.owner.entity.GoodsDetailModel.SpecListBean.SpecValueListBean;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.entity.MessageContacts;
import com.czy.owner.entity.PushGoodsModel;
import com.czy.owner.entity.SelectStoreModel;
import com.czy.owner.entity.ShoppingCartGoodsModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.ShareEvent;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.ui.message.ChatActivity;
import com.czy.owner.ui.physicalorder.ConfirmOrderActivity;
import com.czy.owner.utils.AssetsFileUtils;
import com.czy.owner.utils.CartAnim;
import com.czy.owner.utils.GsonUtils;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.ShareUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.glide.GlideUtils;
import com.czy.owner.widget.TagsLayout;
import com.easemob.cases.MessageHelper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import q.rorbin.badgeview.QBadgeView;

import static com.czy.owner.ui.physicalorder.ConfirmOrderActivity.ConfirmOrderJumpType.CART;
import static com.czy.owner.ui.physicalorder.ConfirmOrderActivity.TRANSMISSION_GOODS_TYPE_KEY;
import static com.hyphenate.easeui.domain.EaseUser.USER_TYPE_SYSTEM;

/**
 * Created by asus
 * 商品详情
 */
public class GoodsDetailActivity extends BaseActivity {
    private static final int SELECT_STORE_TYPE = 0x11;
    private static final int SEC_KILL_TYPE = 1;
    @BindView(R.id.rl_goods_detail)
    RelativeLayout rlGoodsDetail;
    @BindView(R.id.webview)
    WebView webView;
    @BindView(R.id.myProgressBar)
    ProgressBar myProgressBar;
    @BindView(R.id.iv_customer_service)
    ImageView ivCustomerService;
    @BindView(R.id.ll_buycar)
    LinearLayout llBuyCar;
    @BindView(R.id.bottom_goods_detail)
    LinearLayout bottomGoodsDetail;
    @BindView(R.id.iv_buycar)
    ImageView ivBuycar;
    @BindView(R.id.cb_collection_normal)
    CheckBox cbCollectionNormal;
    @BindView(R.id.tv_add_buycar)
    TextView tvAddBuycar;
    @BindView(R.id.tv_buy_now)
    TextView tvBuyNow;
    @BindView(R.id.tv_page_title_center)
    TextView tvPageTitleCenter;
    @BindView(R.id.btn_page_title_right)
    Button btnPageTitleRight;
    private PopupWindow popWnd;
    private List<GoodsDetailModel.SpecListBean> specList = new ArrayList<>();
    private List<GoodsDetailModel.SpecGroupListBean> specGroupList = new ArrayList<>();
    private ArrayList<SpecValueListBean> selectSpecValueListBean = new ArrayList<>();
    private ArrayList<TextView> tvTabs = new ArrayList<>();
    private ArrayList<TextView> tvPostageTabs = new ArrayList<>();
    private ArrayList<GoodsEntity> goodsEntities = new ArrayList<GoodsEntity>();
    private GoodsDetailModel goodsDetailModel;
    private GoodsEntity goods;
    private LinearLayout llNoBindStore;
    private LinearLayout llBindStore;
    private TextView tvServiceStore;
    private TextView tvInstallMoney;
    private ImageView ivSelectStore;
    private QBadgeView qb;
    private ArrayList<String> loadHistoryUrls = new ArrayList<String>();
    private String mLastUrl;
    private GoodsDetailModel mModel;
    private RxPermissions rxPermissions;
    private boolean isSecKill = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
        RxBus.getDefault().unRegister(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_goods_detail;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        btnPageTitleRight.setVisibility(View.VISIBLE);
        btnPageTitleRight.setBackgroundResource(R.mipmap.icon_share);
        if (getIntent().getBooleanExtra("isHide", false)) {
            bottomGoodsDetail.setVisibility(View.GONE);
//            btnPageTitleRight.setVisibility(View.GONE);
        }
        RxBus.getDefault().register(this);
        rxPermissions = new RxPermissions(GoodsDetailActivity.this);
        getPermissions();
        tvPageTitleCenter.setText(R.string.goods_detail);
        qb = new QBadgeView(GoodsDetailActivity.this);

        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                WebView.HitTestResult hr = ((WebView) view).getHitTestResult();
                if (hr != null && loadHistoryUrls != null) {
                    if (loadHistoryUrls.size() < 1) {
                        if (loadHistoryUrls.isEmpty() || !loadHistoryUrls.get(loadHistoryUrls.size() - 1).equals(mLastUrl)) {
                            loadHistoryUrls.add(mLastUrl);
                        }
                    }


                }
                return false;
            }
        });
        getGoodsData();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        //判断是否可以返回操作
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            //过滤是否为重定向后的链接
            if (loadHistoryUrls.size() > 0) {
                loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
                //加载重定向之前的页
                if (goodsDetailModel != null) {
                    webView.loadUrl(Constants.APP_API_BASE + goodsDetailModel.getGoodsViewUrl());
                }

            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    public void getPermissions() {
        rxPermissions.request(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            MyLog.e("yang", "获取权限成功");

                        } else {
                            MyLog.e("yang", "获取权限失败");
                        }
                    }
                });
    }

    private void getGoodsData() {
        final GoodsDetailApi goodsDetailApi = new GoodsDetailApi(new HttpOnNextListener<GoodsDetailModel>() {
            @Override
            public void onNext(final GoodsDetailModel model) {
                mModel = model;
                initWebView(model.getGoodsViewUrl());
                specList = model.getSpecList();
                specGroupList = model.getSpecGroupList();
                goodsDetailModel = model;
                boolean isContainerSecKill = false;
                for (int i = 0; i < specGroupList.size() && !isContainerSecKill; i++) {
                    if (specGroupList.get(i).getSecondsKill() != null && specGroupList.get(i).getSecondsKill().getCurrentCount() > 0) {
                        isContainerSecKill = true;
                    }
                }
                //如果支持线下付款，置灰加入购物车 goodsDetailModel.isSupportLinePay()
                if (isContainerSecKill || goodsDetailModel.isSupportLinePay()) {
                    tvAddBuycar.setBackgroundColor(getResources().getColor(R.color.appTipColor));
                    tvAddBuycar.setClickable(false);
                }
                tvAddBuycar.setVisibility(View.VISIBLE);
                if (getIntent().getBooleanExtra("isHide", false)) {
                    bottomGoodsDetail.setVisibility(View.GONE);
                } else {
                    bottomGoodsDetail.setVisibility(View.VISIBLE);

                }

                GoodsEntity goodsEntity = new GoodsEntity();
                goodsEntity.setGoodsId(goodsDetailModel.getGoodsId());
                goodsEntity.setCount(1);
                isSecKill = isContainerSecKill;
                goodsEntity.setGoodsLogo(goodsDetailModel.getGoodsLogo());
                goodsEntity.setGoodsName(goodsDetailModel.getGoodsName());
                goodsEntity.setBelongsType(goodsDetailModel.getBelongsType());

                if ("store".equals(goodsDetailModel.getBelongsType())) {
                    if (goodsDetailModel.getFromGoodsId() > 0) {
                        goodsEntity.setBelongsType("transfer");
                    }
                }
                goodsEntity.setFromGoodsId(goodsDetailModel.getFromGoodsId());

                goodsEntity.setLogisticsTemplateId(goodsDetailModel.getLogisticsTemplateId());
                goodsEntity.setInstall(goodsDetailModel.isInstall());
                goodsEntities.add(goodsEntity);

                qb.bindTarget(llBuyCar).setBadgeGravity(Gravity.END | Gravity.TOP).setGravityOffset(0, 6, true);
                if (goodsDetailModel != null && goodsDetailModel.getShoppingCartGoodsCount() != 0) {
                    qb.setBadgeNumber(goodsDetailModel.getShoppingCartGoodsCount());
                }
                cbCollectionNormal.setChecked(goodsDetailModel.isIsCollect());
                cbCollectionNormal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (UserHelper.getInstance().isLogined()) {
                            requestCollect("goods", model.getGoodsId() + "", (isChecked ? "" : "delCollect"), isChecked);
                        } else {
                            cbCollectionNormal.setChecked(false);
                            startActivity(new Intent(GoodsDetailActivity.this, LoginActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, false));
                        }

                    }
                });


            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);
        goodsDetailApi.setSession(UserHelper.getInstance().getSession(GoodsDetailActivity.this));

        if (getIntent().getIntExtra("fromType", 0) == SEC_KILL_TYPE) {
            goodsDetailApi.setSpecGroupKey(getIntent().getStringExtra("specGroupKey"));
        } else {
            goodsDetailApi.setGoodsId(getIntent().getIntExtra("goodsId", 0) + "");
        }
        goodsDetailApi.setStoreId(SharedPreferencesUtils.getParam(GoodsDetailActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(goodsDetailApi);
    }

    private void requestCollect(String collectType, String collectTypeId, String type, final boolean isMCollect) {
        CollectionApi collectionApi = new CollectionApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String o) {
                if (isMCollect) {
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "收藏成功");
                    cbCollectionNormal.setChecked(true);
                } else {
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "取消成功");
                    cbCollectionNormal.setChecked(false);
                }
                RxBus.getDefault().post(Constants.RXBUS_CODE_GOODS_COLLECTION, new CommonEvent("收藏"));
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        }, this);
        collectionApi.setSession(UserHelper.getInstance().getSessionInfoModel(GoodsDetailActivity.this).getSession());
        collectionApi.setCollectType(collectType);
        collectionApi.setCollectTypeId(collectTypeId);
        collectionApi.setType(type);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(collectionApi);


    }

    private void initWebView(String url) {
        WebSettings settings = webView.getSettings();
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);
        webView.loadUrl(Constants.APP_API_BASE + url + "&session=" + UserHelper.getInstance().getSession(GoodsDetailActivity.this));
        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    myProgressBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == myProgressBar.getVisibility()) {
                        myProgressBar.setVisibility(View.VISIBLE);
                    }
                    myProgressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }


        });
    }

    @OnClick(R.id.btn_page_title_left)
    public void onViewClicked() {
        if (webView.canGoBack()) {
            //过滤是否为重定向后的链接
            if (loadHistoryUrls.size() > 0) {
                loadHistoryUrls.remove(loadHistoryUrls.get(loadHistoryUrls.size() - 1));
                //加载重定向之前的页
                if (goodsDetailModel != null) {
                    webView.loadUrl(Constants.APP_API_BASE + goodsDetailModel.getGoodsViewUrl());
                }
            } else {
                finish();
            }
        } else {
            finish();
        }

    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, final String url) {
            view.loadUrl(url);
            MyLog.e("yang", "url===" + url);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    http://118.178.188.69/pub/getCoupons?couponsGroupId=745
                    if (url.contains("pub/getCoupons?couponsGroupId")) {
                        webView.stopLoading();
                        if (!UserHelper.getInstance().isLogined()) {
                            MyLog.e("yang", "未登录");
                            startActivity(new Intent(GoodsDetailActivity.this, LoginActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, false));
                        } else {
                            String mPattern = "pub/getCoupons\\?couponsGroupId=(\\d*?)$";
                            Pattern p = Pattern.compile(mPattern);
                            Matcher m = p.matcher(url);
                            while (m.find()) {
                                String couponsGroupId = m.group(1);
                                MyLog.e("yang", "couponsGroupId===" + couponsGroupId);
                                getCouponsData(couponsGroupId);
                                webView.reload();
                            }
                        }


                    }

                }
            });

            return true;


        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            mLastUrl = url;

        }
    }

    @Override
    protected void updateViews() {

    }

    @Subscribe(code = Constants.GOODS_SHARE_CODE, threadMode = ThreadMode.MAIN)
    public void event(ShareEvent shareEvent) {
        if ("1".equals(shareEvent.getType()) && !StringUtils.isEmpty(UserHelper.getInstance().getSession(this))) {
            PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "分享成功");
            getGoodsShare();
        }

    }

    public void getGoodsShare() {
        if (mModel == null)
            return;
        GoodsShareApi goodsShareApi = new GoodsShareApi(new HttpOnNextListener<Object>() {
            @Override
            public void onNext(Object s) {

            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        goodsShareApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        goodsShareApi.setGoodsId(mModel.getGoodsId() + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(goodsShareApi);
    }

    private void showShareDialog(String imgUrl, String title, String content, String goodsId, String specGroupKey) {
        if (TextUtils.isEmpty(imgUrl)) {
            try {
                AssetsFileUtils.getImageFromAssetsFile(GoodsDetailActivity.this, "owner_share_icon.png");
            } catch (Exception e) {

            }
        }
        String shareUrl = Constants.APP_API_BASE + Constants.PUB_GOODSVIEW + "?goodsId=" + goodsId + "&specGroupKey=" + specGroupKey;
        ShareUtils.showShare(this, null, false, shareUrl, PhoneUtils.delHTMLTag(title), PhoneUtils.delHTMLTag(title), imgUrl, shareUrl);
    }


    @OnClick({R.id.btn_page_title_right, R.id.iv_customer_service, R.id.iv_buycar, R.id.tv_add_buycar, R.id.tv_buy_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_page_title_right:
                if (mModel != null)
                    showShareDialog(mModel.getGoodsLogo(), mModel.getGoodsName(), mModel.getGoodsDetails(), mModel.getGoodsId() + "", "");
                break;

            case R.id.iv_customer_service:
                if (checkLoginState() && goodsDetailModel != null) {
                    pushGoods(this, Integer.parseInt(goodsDetailModel.getBelongsTypeId()), goodsDetailModel.getFromGoodsId());

                }
                break;
            case R.id.iv_buycar:
                if (checkLoginState()) {
                    startActivity(new Intent(GoodsDetailActivity.this, ShoppingCartActivity.class));
                }
                break;
            case R.id.tv_add_buycar:
                if (checkLoginState()) {
                    showGoodsStandard(view, true);
                }
                break;
            case R.id.tv_buy_now:
                if (checkLoginState()) {
                    showGoodsStandard(view, false);
                }
                break;
        }
    }

    private void showGoodsStandard(View v, final boolean isAddToCart) {
        if (goodsEntities.size() > 0) {
            goods = goodsEntities.get(0);
            tvTabs.clear();
            tvPostageTabs.clear();
            if (goodsDetailModel != null) {
                View contentView = LayoutInflater.from(GoodsDetailActivity.this).inflate(R.layout.pop_goods_standard, null);

                popWnd = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                contentView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (popWnd != null && popWnd.isShowing()) {
                            popWnd.dismiss();
                        }
                        return false;
                    }
                });
                ColorDrawable cd = new ColorDrawable(0x77000000);
                popWnd.setBackgroundDrawable(cd);
                // 设置点击窗口外边窗口消失
                popWnd.setOutsideTouchable(true);
                // 设置此参数获得焦点，否则无法点击
                popWnd.setFocusable(true);
                popWnd.setAnimationStyle(R.style.ShoppingCartPopupAnimation);
                popWnd.setClippingEnabled(true);
                LinearLayout llContainer = (LinearLayout) contentView.findViewById(R.id.ll_container);
                final TextView tvSure = (TextView) contentView.findViewById(R.id.tv_sure);
                final TextView tvPopChooseSpec = (TextView) contentView.findViewById(R.id.tv_pop_choose_spec);
                final TextView tvGoodsPrice = (TextView) contentView.findViewById(R.id.tv_goods_price);
                final ImageView ivPopGoodsDetail = (ImageView) contentView.findViewById(R.id.iv_pop_goods_detail);
                ImageView ivGoodsMinus = (ImageView) contentView.findViewById(R.id.iv_goods_minus);
                final TextView tvGoodsCount = (TextView) contentView.findViewById(R.id.tv_goods_count);
                final TextView tvMemberPrice = (TextView) contentView.findViewById(R.id.tv_member_price);
                ImageView ivGoodsAdd = (ImageView) contentView.findViewById(R.id.iv_goods_add);
                tvGoodsPrice.setText("¥" + String.format("%.2f", goodsDetailModel.getPrice()));
                GlideUtils.loadImage(this, goodsDetailModel.getGoodsLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                    @Override
                    public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                    }

                    @Override
                    public void onLoadingError(String source, Exception e) {

                    }
                }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                if (specList.size() == 0) {
                    tvPopChooseSpec.setText("无规格");
                    if (specGroupList.size() > 0) {
                        GoodsDetailModel.SecondsKill secondsKill = specGroupList.get(0).getSecondsKill();
                        goods.setToStorePrice(specGroupList.get(0).getToStorePrice());
                        if (secondsKill != null) {
                            goods.setGoodsSpecValuesPrice(secondsKill.getKillPrice());
                            goods.setSecKill(true);
                        } else {
                            goods.setSecKill(false);
                            goods.setGoodsSpecValuesPrice(specGroupList.get(0).getGoodsSpecValuesPrice());
                        }
                        goods.setHaveMemberPriceFlag(specGroupList.get(0).isHaveMemberPriceFlag());
                        goods.setStoreMemberPrice(specGroupList.get(0).getStoreMemberPrice());
                        goods.setGoodsSpecId(specGroupList.get(0).getIdGroup());
                        goods.setSpecGroupKey(specGroupList.get(0).getSpecGroupKey());
                        goods.setGoodsSpecValuesId(specGroupList.get(0).getGoodsSpecValuesId());
                        tvGoodsPrice.setText("¥" + String.format("%.2f", goods.getGoodsSpecValuesPrice()));
                        if (specGroupList.get(0).isHaveMemberPriceFlag()) {
                            tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", goods.getStoreMemberPrice()));
                        } else {
                            tvMemberPrice.setText("");
                        }
                    }
                }
                for (int i = 0; i < specList.size(); i++) {
                    View addView = LayoutInflater.from(GoodsDetailActivity.this).inflate(R.layout.include_goods_standard, null);
                    TagsLayout tagsClassify = (TagsLayout) addView.findViewById(R.id.tags_classify);
                    TextView tvClassifyName = (TextView) addView.findViewById(R.id.tv_classify_name);
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tvClassifyName.setText(specList.get(i).getSpecName());
                    for (int j = 0; j < specList.get(i).getSpecValueList().size(); j++) {
                        View tagView = View.inflate(GoodsDetailActivity.this, R.layout.item_screen_brand, null);
                        TextView textView = (TextView) tagView.findViewById(R.id.tv_screen_item);
                        SpecValueListBean tag = specList.get(i).getSpecValueList().get(j);
                        textView.setText(tag.getSpecValue());
                        textView.setTag(tag);
                        tvTabs.add(textView);
                        tagView.setTag(false);
                        tagView.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                TextView tv = (TextView) v.findViewById(R.id.tv_screen_item);
                                updateSelectTabs(tv, tvGoodsPrice, tvPopChooseSpec, ivPopGoodsDetail, tvMemberPrice);
                            }
                        });
                        tagsClassify.addView(tagView, lp);
                    }
                    llContainer.addView(addView);
                }
                goods.setInstall(goodsDetailModel.isInstall());
                goods.setPostageFree(goodsDetailModel.isPostageFree());
                if (goodsDetailModel.isInstall()) {
                    View chooseServiceView = LayoutInflater.from(GoodsDetailActivity.this).inflate(R.layout.include_service_choose, null);
                    final GoodsDetailModel.InstallStoreBean installStore = goodsDetailModel.getInstallStore();
                    RadioGroup rgChooseService = (RadioGroup) chooseServiceView.findViewById(R.id.rg_choose_service);
                    final LinearLayout llStoreInfo = (LinearLayout) chooseServiceView.findViewById(R.id.ll_store_info);
                    llNoBindStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_no_bind_store);
                    final LinearLayout llChooseStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_choose_store);
                    llBindStore = (LinearLayout) chooseServiceView.findViewById(R.id.ll_bind_store);
                    tvServiceStore = (TextView) chooseServiceView.findViewById(R.id.tv_service_store);
                    tvInstallMoney = (TextView) chooseServiceView.findViewById(R.id.tv_install_money);
                    RadioButton rbNeedService = (RadioButton) chooseServiceView.findViewById(R.id.rb_need_service);
                    RadioButton rbNoService = (RadioButton) chooseServiceView.findViewById(R.id.rb_no_service);
                    ivSelectStore = (ImageView) chooseServiceView.findViewById(R.id.iv_select_store);
                    if (!goods.isEverChooseInstall()) {
                        if (installStore != null) {
                            llStoreInfo.setVisibility(View.VISIBLE);
                            rbNeedService.setChecked(true);
                            llNoBindStore.setVisibility(View.GONE);
                            llBindStore.setVisibility(View.VISIBLE);
                            ivSelectStore.setVisibility(View.GONE);
                            String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + String.format("%.2f", goodsDetailModel.getServicePrice()) + "</font>";
                            tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                            goods.setServiceInstallStoreId(installStore.getStoreId());
                            goods.setInstallStoreName(installStore.getStoreName());
                            goods.setNeedInstall(true);
                            goods.setInstall(true);
                            goods.setServicePrice(goodsDetailModel.getServicePrice());
                            goods.setEverChooseInstall(true);
                            tvInstallMoney.setText(Html.fromHtml(title));
                        } else {
                            llStoreInfo.setVisibility(View.GONE);
                            ivSelectStore.setVisibility(View.GONE);
                        }
                    } else {
                        if (goods.isNeedInstall()) {
                            rbNeedService.setChecked(true);
                            llStoreInfo.setVisibility(View.VISIBLE);
                            if (installStore != null) {
                                llNoBindStore.setVisibility(View.GONE);
                                llBindStore.setVisibility(View.VISIBLE);
                                String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + String.format("%.2f", goodsDetailModel.getServicePrice()) + "</font>";
                                goods.setServiceInstallStoreId(installStore.getStoreId());
                                goods.setInstallStoreName(installStore.getStoreName());
                                goods.setServicePrice(goodsDetailModel.getServicePrice());
                                tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                                tvInstallMoney.setText(Html.fromHtml(title));
                                ivSelectStore.setVisibility(View.GONE);
                            } else if (goods.getServiceInstallStoreId() != 0 && installStore == null) {
                                llNoBindStore.setVisibility(View.GONE);
                                llBindStore.setVisibility(View.VISIBLE);
                                ivSelectStore.setVisibility(View.VISIBLE);
                                String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + String.format("%.2f", goods.getServicePrice()) + "</font>";
                                tvServiceStore.setText("服务门店:" + goods.getInstallStoreName());
                                tvInstallMoney.setText(Html.fromHtml(title));
                            } else {
                                llNoBindStore.setVisibility(View.VISIBLE);
                                llBindStore.setVisibility(View.GONE);
                            }
                        } else {
                            llStoreInfo.setVisibility(View.GONE);
                            rbNoService.setChecked(true);
                        }
                    }

                    llChooseStore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(GoodsDetailActivity.this, SelectServiceStoreActivity.class);
                            intent.putExtra("goodsId", goodsDetailModel.getGoodsId() + "");
                            startActivityForResult(intent, SELECT_STORE_TYPE);
                        }
                    });
                    llBindStore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (ivSelectStore.getVisibility() == View.VISIBLE) {
                                Intent intent = new Intent(GoodsDetailActivity.this, SelectServiceStoreActivity.class);
                                intent.putExtra("goodsId", goodsDetailModel.getGoodsId() + "");
                                startActivityForResult(intent, SELECT_STORE_TYPE);
                            }
                        }
                    });
                    rgChooseService.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId) {
                                case R.id.rb_need_service:
                                    goods.setInstall(true);
                                    goods.setNeedInstall(true);
                                    goods.setEverChooseInstall(true);
                                    llStoreInfo.setVisibility(View.VISIBLE);
                                    if (installStore != null) {
                                        llNoBindStore.setVisibility(View.GONE);
                                        llBindStore.setVisibility(View.VISIBLE);
                                        goods.setServiceInstallStoreId(installStore.getStoreId());
                                        goods.setInstallStoreName(installStore.getStoreName());
                                        goods.setServicePrice(goodsDetailModel.getServicePrice());
                                        String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + goodsDetailModel.getServicePrice() + "</font>";
                                        tvServiceStore.setText("服务门店:" + installStore.getStoreName());
                                        tvInstallMoney.setText(Html.fromHtml(title));
                                        ivSelectStore.setVisibility(View.GONE);

                                    } else if (goods.getServiceInstallStoreId() != 0 && installStore == null) {
                                        llNoBindStore.setVisibility(View.GONE);
                                        llBindStore.setVisibility(View.VISIBLE);
                                        ivSelectStore.setVisibility(View.VISIBLE);
                                        String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + String.format("%.2f", goods.getServicePrice()) + "</font>";
                                        tvServiceStore.setText("服务门店:" + goods.getInstallStoreName());
                                        tvInstallMoney.setText(Html.fromHtml(title));
                                    } else {
                                        llNoBindStore.setVisibility(View.VISIBLE);
                                        llBindStore.setVisibility(View.GONE);
                                    }
                                    break;
                                case R.id.rb_no_service:
                                    goods.setNeedInstall(false);
                                    goods.setEverChooseInstall(true);
                                    llStoreInfo.setVisibility(View.GONE);
                                    break;
                            }
                        }
                    });
                    llContainer.addView(chooseServiceView);
                }
                if (!goodsDetailModel.isPostageFree()) {
                    List<DeliveryWayEntity> deliveryWayList = goodsDetailModel.getDeliveryWayList();
                    View addView = LayoutInflater.from(GoodsDetailActivity.this).inflate(R.layout.include_goods_standard, null);
                    TagsLayout tagsClassify = (TagsLayout) addView.findViewById(R.id.tags_classify);
                    TextView tvClassifyName = (TextView) addView.findViewById(R.id.tv_classify_name);
                    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    tvClassifyName.setText("配送方式");

                    if (deliveryWayList.size() == 1) {
                        goods.setDeliveryWay(deliveryWayList.get(0));
                    } else {
                        for (int j = 0; j < deliveryWayList.size(); j++) {
                            View tagView = View.inflate(GoodsDetailActivity.this, R.layout.item_screen_brand, null);
                            TextView textView = (TextView) tagView.findViewById(R.id.tv_screen_item);
                            DeliveryWayEntity deliveryWayListBean = deliveryWayList.get(j);
                            textView.setText(deliveryWayListBean.getName());
                            textView.setTag(deliveryWayListBean);
                            tvPostageTabs.add(textView);
                            tagView.setTag(false);
                            tagView.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    TextView tv = (TextView) v.findViewById(R.id.tv_screen_item);
                                    updatePostageView(tv);
                                }
                            });
                            tagsClassify.addView(tagView, lp);
                        }
                        llContainer.addView(addView);
                    }


                }


                ivGoodsMinus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (goods.getCount() > 1) {
                            goods.setCount(goods.getCount() - 1);
                            tvGoodsCount.setText(goods.getCount() + "");
                        } else {
                            goods.setCount(1);
                            tvGoodsCount.setText("1");
                        }
                    }
                });
                ivGoodsAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goods.setCount(goods.getCount() + 1);
                        tvGoodsCount.setText(goods.getCount() + "");
                    }
                });
                tvSure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        AddToCartRequest(tvAddBuycar, ivPopGoodsDetail, ivBuycar, rlGoodsDetail, goods, isAddToCart);

                    }
                });
                initPopTabs(tvGoodsPrice, tvPopChooseSpec, tvGoodsCount, goods, ivPopGoodsDetail, tvMemberPrice);
                popWnd.showAtLocation(v, Gravity.BOTTOM, 0, 0);

            }


        }

    }

    private void AddToCartRequest(final TextView tvAddBuycar, final ImageView ivPopGoodsDetail, final ImageView ivBuycar, final RelativeLayout rlGoodsDetail, GoodsEntity goodsEntity, boolean isAddToCart) {
        MyLog.e("asus", goods.isHaveMemberPriceFlag() + "--->" + goods.getStoreMemberPrice());

        boolean isCanDissmiss = true;
        AddCartApi addCartApi = new AddCartApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String o) {
                popWnd.dismiss();
                PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "加入购物车成功");
                CartAnim.AddToCart(tvAddBuycar, ivPopGoodsDetail, ivBuycar, GoodsDetailActivity.this, rlGoodsDetail, 1);
                getCartCount();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);
        addCartApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        addCartApi.setGoodsId(goodsEntity.getGoodsId() + "");
        addCartApi.setCount(goodsEntity.getCount() + "");
        if (specList.size() == 0) {
            addCartApi.setSpecGroupKey(goodsDetailModel.getSpecGroupList().get(0).getSpecGroupKey() + "");
        } else {
            if (specList.size() == selectSpecValueListBean.size()) {
                if (TextUtils.isEmpty(goods.getSpecGroupKey())) {
                    isCanDissmiss = false;
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "请选择商品规格");
                    return;
                } else {
                    addCartApi.setSpecGroupKey(goods.getSpecGroupKey());
                }
            } else {
                isCanDissmiss = false;
                if (selectSpecValueListBean.size() == 0) {
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "请选择商品" + specList.get(0).getSpecName());
                } else {
                    boolean specContainer = true;
                    for (int i = 0; i < specList.size() && specContainer; i++) {
                        int specId = specList.get(i).getSpecId();
                        for (int j = 0; j < selectSpecValueListBean.size(); j++) {
                            if (selectSpecValueListBean.get(j).getSpecId() == specId) {
                                break;
                            } else {
                                if (j == selectSpecValueListBean.size() - 1) {
                                    specContainer = false;
                                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "请选择商品" + specList.get(i).getSpecName());
                                    break;
                                }
                            }

                        }
                    }
                }
            }
        }

        boolean postageFree = goodsEntity.isPostageFree();
        if (!postageFree) {
            if (goodsEntity.getDeliveryWay() != null) {
                addCartApi.setDeliveryWayId(goodsEntity.getDeliveryWay().getDeliveryWayId() + "");
            } else {
                isCanDissmiss = false;
                PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "请选择配送方式");
            }


        }
        if (goodsEntity.isInstall()) {
            if (goodsEntity.getServiceInstallStoreId() != 0 && goodsEntity.isNeedInstall()) {
                addCartApi.setServiceInstallStoreId(goodsEntity.getServiceInstallStoreId() + "");
                addCartApi.setNeedService("true");
            } else if (goodsEntity.isNeedInstall() && goodsEntity.getServiceInstallStoreId() == 0) {
                isCanDissmiss = false;
                PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "请选择安装门店");
            } else {
                addCartApi.setNeedService("true");
            }


        } else {
            addCartApi.setNeedService("false");
        }


        if (isAddToCart) {
            if (isCanDissmiss) {

                if (goodsEntity.getBelongsType().equals("system")) {
                    //    2018.3.30 wangyang 新增 商品来源
                String mFromType = getIntent().getStringExtra("mFromType");
                String mFromTypeId = getIntent().getStringExtra("mFromTypeId");
                    MyLog.e("yang","GoodDetail=="+ mFromTypeId);
                    if (!TextUtils.isEmpty(mFromTypeId)) {
                        addCartApi.setFromTypeId(mFromTypeId);
                    }
                    if (!TextUtils.isEmpty(mFromType)) {
                        addCartApi.setFromType(mFromType);
                    }
                }

                HttpManager manager = HttpManager.getInstance();
                manager.doHttpDeal(addCartApi);
            }
        } else {
            if (isCanDissmiss) {
                List<GoodsEntity> list = new ArrayList<>();
                list.add(goods);
                getGoodsRemainCount(list);

            }

        }


    }

    private void updatePostageView(TextView tv) {
        goods.setDeliveryWay((DeliveryWayEntity) tv.getTag());
        for (int i = 0; i < tvPostageTabs.size(); i++) {
            DeliveryWayEntity deliveryWayEntity = (DeliveryWayEntity) tvPostageTabs.get(i).getTag();
            if (deliveryWayEntity.getDeliveryWayId() == goods.getDeliveryWay().getDeliveryWayId()) {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
            } else {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
            }

        }


    }

    private void updateSelectTabs(TextView tv, TextView tvGoodsPrice, TextView tvPopChooseSpec, ImageView ivPopGoodsDetail, TextView tvMemberPrice) {
        SpecValueListBean tag = (SpecValueListBean) tv.getTag();
        if (!selectSpecValueListBean.contains(tag)) {
            for (int i = 0; i < selectSpecValueListBean.size(); i++) {
                if (selectSpecValueListBean.get(i).getSpecId() == tag.getSpecId()) {
                    selectSpecValueListBean.remove(i);
                    break;
                }
            }
            selectSpecValueListBean.add(tag);
        }
        int tagSpecId = tag.getSpecId();
        int tagSpecValuesId = tag.getSpecValuesId();
        for (int i = 0; i < tvTabs.size(); i++) {
            SpecValueListBean tabsTag = (SpecValueListBean) tvTabs.get(i).getTag();
            if (tabsTag.getSpecId() == tagSpecId) {
                if (tagSpecValuesId == tabsTag.getSpecValuesId()) {
                    tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                    tvTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
                } else {
                    tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                    tvTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
                }
            }
        }
        //1:15,2:16
        Collections.sort(selectSpecValueListBean, new SortBySpecId());
        StringBuilder keyGroup = new StringBuilder();
        StringBuilder specSB = new StringBuilder();
        for (int i = 0; i < selectSpecValueListBean.size(); i++) {
            if (i == selectSpecValueListBean.size() - 1) {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId());
                specSB.append(selectSpecValueListBean.get(i).getSpecValue());

            } else {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId() + ",");
                specSB.append(selectSpecValueListBean.get(i).getSpecValue() + ",");
            }

        }
        boolean isContaineSpec = false;
        if (selectSpecValueListBean.size() > 0) {
            for (int i = 0; i < specGroupList.size(); i++) {

                if (specGroupList.get(i).getIdGroup().equals((keyGroup + ""))) {
                    isContaineSpec = true;
                    if (TextUtils.isEmpty(specGroupList.get(i).getLogo())) {
                        GlideUtils.loadImage(this, goodsDetailModel.getGoodsLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    } else {
                        goods.setGoodsLogo(specGroupList.get(i).getLogo());
                        GlideUtils.loadImage(this, specGroupList.get(i).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    }

                    GoodsDetailModel.SecondsKill secondsKill = specGroupList.get(i).getSecondsKill();
                    if (secondsKill != null) {
                        tvGoodsPrice.setText("¥" + String.format("%.2f", secondsKill.getKillPrice()));
                        goods.setGoodsSpecValuesPrice(secondsKill.getKillPrice());
                        goods.setSecKill(true);
                    } else {
                        tvGoodsPrice.setText("¥" + String.format("%.2f", specGroupList.get(i).getGoodsSpecValuesPrice()));
                        goods.setGoodsSpecValuesPrice(specGroupList.get(i).getGoodsSpecValuesPrice());
                        goods.setSecKill(false);
                    }

                    goods.setHaveMemberPriceFlag(specGroupList.get(i).isHaveMemberPriceFlag());
                    goods.setStoreMemberPrice(specGroupList.get(i).getStoreMemberPrice());
                    if (specGroupList.get(i).isHaveMemberPriceFlag()) {
                        tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", goods.getStoreMemberPrice()));
                    } else {
                        tvMemberPrice.setText("");
                    }
                    goods.setGoodsSpecId(keyGroup + "");
                    goods.setGoodsSpec(specGroupList.get(i).getGoodsSpec());
                    goods.setGoodsSpecValuesId(specGroupList.get(i).getGoodsSpecValuesId());
                    goods.setSpecGroupKey(specGroupList.get(i).getSpecGroupKey());
                    goods.setToStorePrice(specGroupList.get(i).getToStorePrice());
                } else {
                    if (selectSpecValueListBean.size() == specList.size()) {
                        if (i == specGroupList.size() - 1 && !isContaineSpec) {
                            goods.setSpecGroupKey("");
                            PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "无该规格商品!");
                        }

                    }

                }
                tvPopChooseSpec.setText("已选择" + specSB);
            }
        }
    }

    private void initPopTabs(TextView tvGoodsPrice, TextView tvPopChooseSpec, TextView tvGoodsCount, GoodsEntity goodsEntity, ImageView ivPopGoodsDetail, TextView tvMemberPrice) {
        tvGoodsCount.setText(goodsEntity.getCount() + "");
        if (getIntent().getIntExtra("fromType", 0) == SEC_KILL_TYPE) {
            goods.setSpecGroupKey(getIntent().getStringExtra("specGroupKey"));
            if (selectSpecValueListBean.size() == 0) {
                for (int i = 0; i < specGroupList.size(); i++) {
                    GoodsDetailModel.SpecGroupListBean specBean = specGroupList.get(i);
                    if (specBean.getSpecGroupKey().equals(getIntent().getStringExtra("specGroupKey"))) {

                        GoodsDetailModel.SecondsKill secondsKill = specGroupList.get(i).getSecondsKill();
                        goods.setGoodsSpecValuesId(specBean.getGoodsSpecValuesId());
                        goods.setGoodsSpec(specBean.getGoodsSpec());
                        goods.setToStorePrice(specBean.getToStorePrice());
                        if (secondsKill != null) {
                            goods.setGoodsSpecValuesPrice(secondsKill.getKillPrice());
                            goods.setSecKill(true);
                        } else {
                            goods.setGoodsSpecValuesPrice(specGroupList.get(i).getGoodsSpecValuesPrice());
                            goods.setSecKill(false);

                        }
                        goods.setHaveMemberPriceFlag(specGroupList.get(i).isHaveMemberPriceFlag());
                        goods.setStoreMemberPrice(specGroupList.get(i).getStoreMemberPrice());
                        if (specGroupList.get(i).isHaveMemberPriceFlag()) {
                            tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", goods.getStoreMemberPrice()));
                        } else {
                            tvMemberPrice.setText("");
                        }
                        if (specList.size() != 0) {
                            for (int j = 0; j < specList.size(); j++) {
                                for (int m = 0; m < specList.get(j).getSpecValueList().size(); m++) {
                                    String[] split = specBean.getIdGroup().split(",");
                                    for (int k = 0; k < split.length; k++) {
                                        if (split[k].split(":").length > 1) {
                                            if (specList.get(j).getSpecValueList().get(m).getSpecValuesId() == Integer.valueOf(split[k].split(":")[1])) {
                                                selectSpecValueListBean.add(specList.get(j).getSpecValueList().get(m));
                                            }

                                        }
                                    }


                                }


                            }
                        }
                    }


                }


            }

        }
        for (int i = 0; i < tvTabs.size(); i++) {
            SpecValueListBean tabsTag = (SpecValueListBean) tvTabs.get(i).getTag();
            for (int j = 0; j < selectSpecValueListBean.size(); j++) {
                SpecValueListBean specValueListBean = selectSpecValueListBean.get(j);
                if (specValueListBean.getSpecId() == tabsTag.getSpecId()) {
                    if (specValueListBean.getSpecValuesId() == tabsTag.getSpecValuesId()) {
                        tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                        tvTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
                    } else {
                        tvTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                        tvTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
                    }

                }


            }

        }
        Collections.sort(selectSpecValueListBean, new SortBySpecId());
        StringBuilder keyGroup = new StringBuilder();
        StringBuilder specSB = new StringBuilder();
        for (int i = 0; i < selectSpecValueListBean.size(); i++) {
            if (i == selectSpecValueListBean.size() - 1) {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId());
                specSB.append(selectSpecValueListBean.get(i).getSpecValue());
            } else {
                keyGroup.append(selectSpecValueListBean.get(i).getSpecId() + ":" + selectSpecValueListBean.get(i).getSpecValuesId() + ",");
                specSB.append(selectSpecValueListBean.get(i).getSpecValue() + ",");
            }
        }
        if (selectSpecValueListBean.size() > 0) {
            for (int i = 0; i < specGroupList.size(); i++) {
                if ((keyGroup + "").equals(specGroupList.get(i).getIdGroup())) {
                    GoodsDetailModel.SecondsKill secondsKill = specGroupList.get(i).getSecondsKill();

                    if (TextUtils.isEmpty(specGroupList.get(i).getLogo())) {
                        GlideUtils.loadImage(this, goodsDetailModel.getGoodsLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    } else {
                        goods.setGoodsLogo(specGroupList.get(i).getLogo());
                        GlideUtils.loadImage(this, specGroupList.get(i).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                            @Override
                            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                            }

                            @Override
                            public void onLoadingError(String source, Exception e) {

                            }
                        }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                    }

                    if (secondsKill != null) {
                        tvGoodsPrice.setText("¥" + String.format("%.2f", secondsKill.getKillPrice()));
                        goods.setGoodsSpecValuesPrice(secondsKill.getKillPrice());
                        goods.setSecKill(true);
                    } else {
                        tvGoodsPrice.setText("¥" + String.format("%.2f", specGroupList.get(i).getGoodsSpecValuesPrice()));
                        goods.setGoodsSpecValuesPrice(specGroupList.get(i).getGoodsSpecValuesPrice());
                        goods.setSecKill(false);
                    }
                    if (specGroupList.get(i).isHaveMemberPriceFlag()) {
                        tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", goods.getStoreMemberPrice()));
                    } else {
                        tvMemberPrice.setText("");
                    }
                    goods.setHaveMemberPriceFlag(specGroupList.get(i).isHaveMemberPriceFlag());
                    goods.setStoreMemberPrice(specGroupList.get(i).getStoreMemberPrice());
                    goods.setGoodsSpec(specGroupList.get(i).getGoodsSpec());
                    goods.setGoodsSpecId(keyGroup + "");
                    goods.setGoodsSpecValuesId(specGroupList.get(i).getGoodsSpecValuesId());
                    goods.setSpecGroupKey(specGroupList.get(i).getSpecGroupKey());
                }
                tvPopChooseSpec.setText("已选择" + specSB);
            }
        } else {
            if (specGroupList.size() > 0) {
                GoodsDetailModel.SecondsKill secondsKill = specGroupList.get(0).getSecondsKill();
                if (secondsKill != null) {
                    goods.setGoodsSpecValuesPrice(secondsKill.getKillPrice());
                    goods.setSecKill(true);
                } else {
                    goods.setGoodsSpecValuesPrice(specGroupList.get(0).getGoodsSpecValuesPrice());
                    goods.setSecKill(false);
                }
                goods.setHaveMemberPriceFlag(specGroupList.get(0).isHaveMemberPriceFlag());
                goods.setStoreMemberPrice(specGroupList.get(0).getStoreMemberPrice());
                if (specGroupList.get(0).isHaveMemberPriceFlag()) {
                    tvMemberPrice.setText("会员价: ¥" + String.format("%.2f", goods.getStoreMemberPrice()));
                } else {
                    tvMemberPrice.setText("");
                }
                goods.setGoodsSpec(specGroupList.get(0).getGoodsSpec());
                goods.setGoodsSpecId(specGroupList.get(0).getIdGroup());
                goods.setGoodsSpecValuesId(specGroupList.get(0).getGoodsSpecValuesId());
                goods.setSpecGroupKey(specGroupList.get(0).getSpecGroupKey());
                if (TextUtils.isEmpty(specGroupList.get(0).getLogo())) {
                    GlideUtils.loadImage(this, goodsDetailModel.getGoodsLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                        @Override
                        public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                        }

                        @Override
                        public void onLoadingError(String source, Exception e) {

                        }
                    }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                } else {
                    goods.setGoodsLogo(specGroupList.get(0).getLogo());
                    GlideUtils.loadImage(this, specGroupList.get(0).getLogo(), ivPopGoodsDetail, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                        @Override
                        public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {
                        }

                        @Override
                        public void onLoadingError(String source, Exception e) {

                        }
                    }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
                }
            }

        }
        for (int i = 0; i < tvPostageTabs.size(); i++) {
            DeliveryWayEntity deliveryWayEntity = (DeliveryWayEntity) tvPostageTabs.get(i).getTag();
            if (goods.getDeliveryWay() != null && deliveryWayEntity.getDeliveryWayId() == goods.getDeliveryWay().getDeliveryWayId()) {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_app_theme_frame);
            } else {
                tvPostageTabs.get(i).setTextColor(getResources().getColor(R.color.appThemeSubTitleColor));
                tvPostageTabs.get(i).setBackgroundResource(R.drawable.shape_gray_frame);
            }


        }


    }


    private void getCartCount() {
        ShoppingCartCountApi shoppingCartListApi = new ShoppingCartCountApi(new HttpOnNextListener<ShoppingCartGoodsModel>() {
            @Override
            public void onNext(ShoppingCartGoodsModel model) {
                qb.setBadgeNumber(model.getCartCount());
                RxBus.getDefault().post(Constants.REFRESH_SHOPPING_CAR, new CommonEvent("购物车刷新"));
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<ShoppingCartGoodsModel>>() {
                }.getType();
                BaseResultEntity<ShoppingCartGoodsModel> cacheModel = gson.fromJson(cache, type);
                qb.setBadgeNumber(cacheModel.getData().getCartCount());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        shoppingCartListApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        shoppingCartListApi.setStoreId(SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        shoppingCartListApi.setCloseMall(UserHelper.getInstance().isCloseMall() + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(shoppingCartListApi);

    }

    public void getCustomer(final Context mContext, final int storeId, final int fromGoodsId) {
//        mLoadView.ShowLoadView();
//        String url = Constants.APP_API_BASE + Constants.OWN_ORDERS_GETCUSTOMER;
//        RequestParams params = new RequestParams(url);
//        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(this).getSession());
//        if (fromGoodsId != 0) {
//            params.addBodyParameter("storeId", 0 + "");
//        } else {
//            params.addBodyParameter("storeId", storeId + "");
//        }
//
////        PhoneUtils.ShowToastMessage(this, storeId+"");
//        params.addBodyParameter("type", "goods");
//        x.http().post(params, new Callback.CommonCallback<String>() {
//
//            @Override
//            public void onSuccess(String result) {
//                //这个方法不提示错误信息。复制注意
//                String data = PhoneUtils.checkResponseFlagLogin(GoodsDetailActivity.this, result);
//                try {
//                    if (!StringUtils.isEmpty(data)) {
//                        MessageContacts messageContacts = GsonUtils.fromJson(data, MessageContacts.class);
//                        pushGoods(messageContacts, storeId, fromGoodsId);
//                    } else {
//                        PhoneUtils.ShowToastMessage(mContext, mContext.getResources().getString(R.string.customer_service_toast));
//                        pushGoods(null, storeId, fromGoodsId);
//                    }
//                } catch (Exception e) {
//                    Log.e("King", "解析失败!");
//                    e.printStackTrace();
//                }
//
//
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//                mLoadView.CancleLoadView();
//            }
//        });

        GetCustomerApi getCustomerApi = new GetCustomerApi(new HttpOnNextListener<MessageContacts>() {
            @Override
            public void onNext(MessageContacts messageContacts) {
                mLoadView.CancleLoadView();
                if (messageContacts != null && !TextUtils.isEmpty(messageContacts.getImKey())) {
                    EaseUser easeUser = new EaseUser(messageContacts.getImKey());
                    MyLog.e("yang", "storeId===" + storeId);
                    MyLog.e("yang", "fromGoodsId===" + fromGoodsId);
                    MyLog.e("yang", "messageContacts.getName()==" + messageContacts.getName() + "\nmessageContacts.getEmployeesName()==" + messageContacts.getEmployeesName());
                    if (storeId != 0) {
                        if (fromGoodsId != 0) {
                            easeUser.setNick(messageContacts.getName());
                            easeUser.setPhone(messageContacts.getPhone());
                            easeUser.setUserType(USER_TYPE_SYSTEM);
                        } else {
                            easeUser.setNick(messageContacts.getEmployeesName());
                            easeUser.setPhone(messageContacts.getContact());
                        }
                    } else {
                        easeUser.setNick(messageContacts.getName());
                        easeUser.setPhone(messageContacts.getPhone());
                        easeUser.setUserType(USER_TYPE_SYSTEM);
                    }
                    //  easeUser.setNick(storeId == 0 ? messageContacts.getName() : messageContacts.getEmployeesName());
                    easeUser.setAvatar(messageContacts.getUserLogo());
                    easeUser.setEmployeeId("" + messageContacts.getStoreEmployeesId());
                    //  easeUser.setPhone(storeId == 0 ? messageContacts.getPhone() : messageContacts.getContact());

//                    if(storeId == 0){
//                        easeUser.setUserType(USER_TYPE_SYSTEM);
//                    }
                    MessageHelper.getInstance().saveContact(easeUser);
//                    //创建一条文本消息，content为消息文字内容，toChatUsername为对方用户或者群聊的id，后文皆是如此
//                    EMMessage message = EMMessage.createTxtSendMessage("dddd", "adm15556832075");
//                    //如果是群聊，设置chattype，默认是单聊
//                    message.setAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, "goods");
//                    //发送消息
//                    EMClient.getInstance().chatManager().sendMessage(message);
                    Intent intent = new Intent(GoodsDetailActivity.this, ChatActivity.class);
                    intent.putExtra(EaseConstant.EXTRA_USER_ID, messageContacts.getImKey());
                    startActivity(intent);
                }
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mLoadView.CancleLoadView();
                MyLog.e("hep", "====" + e.getMessage());
                PhoneUtils.ShowToastMessage(mContext, "暂无客服信息");
            }
        }, (RxAppCompatActivity) mContext);
        getCustomerApi.setSession(UserHelper.getInstance().getSessionInfoModel(mContext).getSession());
        if (fromGoodsId != 0) {
            getCustomerApi.setStoreId("0");
        } else {
            getCustomerApi.setStoreId(storeId + "");
        }
        getCustomerApi.setType("goods");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(getCustomerApi);
    }

    class SortBySpecId implements Comparator<SpecValueListBean> {
        @Override
        public int compare(SpecValueListBean o1, SpecValueListBean o2) {
            return o1.getSpecId() - o2.getSpecId();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SELECT_STORE_TYPE && resultCode == RESULT_OK) {
            llNoBindStore.setVisibility(View.GONE);
            llBindStore.setVisibility(View.VISIBLE);
            ivSelectStore.setVisibility(View.VISIBLE);
            SelectStoreModel selectStoreModel = (SelectStoreModel) data.getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY);
            String title = "参考安装费: " + "<font color='#09c0c7'>" + "¥" + String.format("%.2f", selectStoreModel.getServicePrice()) + "</font>";
            tvServiceStore.setText("服务门店:" + selectStoreModel.getStoreName());
            tvInstallMoney.setText(Html.fromHtml(title));

            goods.setServiceInstallStoreId(selectStoreModel.getStoreId());
            goods.setInstallStoreName(selectStoreModel.getStoreName());
            goods.setServicePrice(selectStoreModel.getServicePrice());
            goods.setNeedInstall(true);
        }
    }

    public void getGoodsRemainCount(final List<GoodsEntity> list) {
        GoodsRemainCountApi goodsRemainCountApi = new GoodsRemainCountApi(new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                popWnd.dismiss();
                Intent intent = new Intent(GoodsDetailActivity.this, ConfirmOrderActivity.class);
                intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, (Serializable) list);
                intent.putExtra(TRANSMISSION_GOODS_TYPE_KEY, CART);
                intent.putExtra("isShoppingCar","2");
                intent.putExtra("isSecKill", isSecKill);

                //    2018.3.30 wangyang 新增 商品来源
                String mFromType = getIntent().getStringExtra("mFromType");
                String mFromTypeId = getIntent().getStringExtra("mFromTypeId");
                if (!TextUtils.isEmpty(mFromTypeId)) {
                    goods.setFromTypeId(mFromTypeId);
                }
                if (!TextUtils.isEmpty(mFromType)) {
                    goods.setFromType(mFromType);
                }

                startActivity(intent);
            }


            @Override
            public void onError(Throwable e) {
                super.onError(e);
                e.printStackTrace();
            }
        }, this);
        goodsRemainCountApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
        StringBuilder goodsIds = new StringBuilder();
        StringBuilder specKeys = new StringBuilder();
        StringBuilder numbers = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            goodsIds.append((list.size() == (i + 1)) ? list.get(i).getGoodsId() + "" : list.get(i).getGoodsId() + ",");
            specKeys.append((list.size() == (i + 1)) ? list.get(i).getSpecGroupKey() + "" : list.get(i).getSpecGroupKey() + ",");
            numbers.append((list.size() == (i + 1)) ? list.get(i).getCount() + "" : list.get(i).getCount() + ",");
        }

        goodsRemainCountApi.setGoodsIds(goodsIds + "");
        goodsRemainCountApi.setSpecGroupKeys(specKeys + "");
        goodsRemainCountApi.setNumbers(numbers + "");
        goodsRemainCountApi.setFormShoppingCar(false + "");
        HttpManager manager = HttpManager.getInstance();
        manager.doHttpDeal(goodsRemainCountApi);
    }

    //推送客服
    public void pushGoods(final Context mContext, final int storeId, final int fromGoodsId) {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_PUSHGOODS;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(GoodsDetailActivity.this).getSession());
        params.addBodyParameter("goodsId", goodsDetailModel.getGoodsId() + "");
        if (!StringUtils.isEmpty(mModel.getGoodsLogo())) {
            params.addBodyParameter("goodsLogo", goodsDetailModel.getGoodsLogo());
        }
        params.addBodyParameter("minPrice", goodsDetailModel.getPrice() + "");
        MyLog.e("hep", goodsDetailModel.getPrice() + "");
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                //这个方法不提示错误信息。复制注意
                String data = PhoneUtils.checkResponseFlagLogin(GoodsDetailActivity.this, result);
                boolean flag = JsonUtil.getJsonValuesBoolean(result, "flag");
                int errorCode = JsonUtil.getJsonValuesInt(result, "errorCode");
                MyLog.e("hep", result);
                try {
                    if (flag) {
//
                        PushGoodsModel pushGoodsModel = GsonUtils.fromJson(data, PushGoodsModel.class);

                        if (pushGoodsModel != null && !TextUtils.isEmpty(pushGoodsModel.getImKey())) {
                            if (pushGoodsModel.getImKey().contains("adm")) {
                                Intent intent = new Intent(GoodsDetailActivity.this, ChatActivity.class);
                                intent.putExtra(EaseConstant.EXTRA_USER_ID, pushGoodsModel.getImKey());
                                startActivity(intent);
                                mLoadView.CancleLoadView();
                            } else {
                                getCustomer(mContext, storeId, fromGoodsId);
                            }
                        }
                    } else if (errorCode != 110) {
                        PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, GoodsDetailActivity.this.getResources().getString(R.string.customer_service_toast));
                        mLoadView.CancleLoadView();
                    }
                } catch (Exception e) {
                    Log.e("King", "解析失败!");
                    e.printStackTrace();
                    mLoadView.CancleLoadView();
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mLoadView.CancleLoadView();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                mLoadView.CancleLoadView();
            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });


    }

    //推送客服商品
//    public void pushGoods(final MessageContacts messageContacts, final int storeId, final int fromGoodsId) {
//        mLoadView.ShowLoadView();
//        PushGoodsApi pushGoodsApi = new PushGoodsApi(new HttpOnNextListener<Object>() {
//            @Override
//            public void onNext(Object s) {
//                mLoadView.CancleLoadView();
//                if (messageContacts != null && !TextUtils.isEmpty(messageContacts.getImKey())) {
//                    EaseUser easeUser = new EaseUser(messageContacts.getImKey());
//                    MyLog.e("yang", "storeId===" + storeId);
//                    MyLog.e("yang", "fromGoodsId===" + fromGoodsId);
//                    MyLog.e("yang", "messageContacts.getName()==" + messageContacts.getName() + "\nmessageContacts.getEmployeesName()==" + messageContacts.getEmployeesName());
//                    if (storeId != 0) {
//                        if (fromGoodsId != 0) {
//                            easeUser.setNick(messageContacts.getName());
//                            easeUser.setPhone(messageContacts.getPhone());
//                            easeUser.setUserType(USER_TYPE_SYSTEM);
//                        } else {
//                            easeUser.setNick(messageContacts.getEmployeesName());
//                            easeUser.setPhone(messageContacts.getContact());
//                        }
//                    } else {
//                        easeUser.setNick(messageContacts.getName());
//                        easeUser.setPhone(messageContacts.getPhone());
//                        easeUser.setUserType(USER_TYPE_SYSTEM);
//                    }
//                    //  easeUser.setNick(storeId == 0 ? messageContacts.getName() : messageContacts.getEmployeesName());
//                    easeUser.setAvatar(messageContacts.getUserLogo());
//                    easeUser.setEmployeeId("" + messageContacts.getStoreEmployeesId());
//                    //  easeUser.setPhone(storeId == 0 ? messageContacts.getPhone() : messageContacts.getContact());
//
////                    if(storeId == 0){
////                        easeUser.setUserType(USER_TYPE_SYSTEM);
////                    }
////                    MessageHelper.getInstance().saveContact(easeUser);
////                    //创建一条文本消息，content为消息文字内容，toChatUsername为对方用户或者群聊的id，后文皆是如此
////                    EMMessage message = EMMessage.createTxtSendMessage("dddd", "adm15556832075");
////                    //如果是群聊，设置chattype，默认是单聊
////                    message.setAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, "goods");
////                    //发送消息
////                    EMClient.getInstance().chatManager().sendMessage(message);
//                    Intent intent = new Intent(GoodsDetailActivity.this, ChatActivity.class);
//                    intent.putExtra(EaseConstant.EXTRA_USER_ID, messageContacts.getImKey());
//                    startActivity(intent);
//                }
//
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                super.onError(e);
//                e.printStackTrace();
//                mLoadView.CancleLoadView();
//            }
//        }, this);
//        pushGoodsApi.setSession(UserHelper.getInstance().getSessionInfoModel(this).getSession());
//        pushGoodsApi.setGoodsId(getIntent().getIntExtra("goodsId", 0) + "");
//        HttpManager manager = HttpManager.getInstance();
//        manager.doHttpDeal(pushGoodsApi);
//    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserHelper.getInstance().isLogined()) {
            getCartCount();
        }

    }

    @Subscribe(code = Constants.ORDER_PAYMENT_SECCESS_CODE, threadMode = ThreadMode.MAIN)
    public void onPaymentSuccess(CommonEvent event) {
        finish();
    }


    private void getCouponsData(String couponsGroupId) {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_RECEIVECOUPONS;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(GoodsDetailActivity.this).getSession());
        params.addBodyParameter("couponsGroupId", couponsGroupId);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyLog.e("yang", "onSuccess==" + result);
                boolean flag = JsonUtil.getJsonValuesBoolean(result, "flag");
                String exp = JsonUtil.getJsonValuesString(result, "exp");
                if (flag) {
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, "领取成功");
                } else {
                    PhoneUtils.ShowToastMessage(GoodsDetailActivity.this, exp);
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                MyLog.e("yang", "onError==" + ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });
    }
}
